/**
 * @Title:  AuthMechanismActivity.java 
 * @Copyright:  XXX Co., Ltd. Copyright YYYY-YYYY,  All rights reserved 
 * @Description:  TODO<请描述此文件是做什么的> 
 * @author:  MeiYi 
 * @data:  2016-1-5 上午11:24:06 
 * @version:  V1.0 
 */

package artid.com.arttogo.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.adapter.PhotoInquiryAdapter;
import artid.com.arttogo.bean.PhotoInquiryList;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.utils.BitmapUtil;
import artid.com.arttogo.view.pulllist.PullToRefreshLayout;
import artid.com.arttogo.view.pulllist.PullableListView;

/**
 * TODO<拍图查询>
 * 
 * @author 熊彬
 * @data: 2016-1-5 上午11:24:06
 * @version: V1.0
 */
public class PhotoResultActivity extends BaseActivity {
	private Context mContext;
	private PhotoInquiryAdapter adapter;
	PullToRefreshLayout mPullLayout;
	private PullableListView mListView;
	private String BitmapBase64;//图片
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sigmatrix.artworkart.BaseActivity#onCreate(android.os.Bundle)
	 */
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_photo_search);
		
		mTittle = (TextView)findViewById(R.id.tittle_text);
		mContext = PhotoResultActivity.this;
		mListView = (PullableListView) findViewById(R.id.listivew);
		mBack = (ImageView) findViewById(R.id.back_btn);
		mBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		mPullLayout = (PullToRefreshLayout) findViewById(R.id.refresh_view);
		mPullLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {

			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				pageIndex = 1;
				reqData();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				// pullToRefreshLayout.setBottomDismiss();
				pageIndex++;
				reqData();
			}
		});
		mTittle.setText("拍图查询");
		getCutImage();
		//Log.e(fromActivityName);
	}
	
	private void getCutImage(){
		String CutPath=getIntent().getStringExtra("CutPath");
		Bitmap bitmap= BitmapUtil.getBitmapFromPath(CutPath);
		if(bitmap!=null){
			BitmapBase64=BitmapUtil.bitmaptoString(bitmap);
		}
		reqData();
	}
	
	int pageIndex =  1,pagesize = 20;
	JSONObject mainJson;
	JSONArray mainJSONArr;
	void reqData(){
		VolleyBasic.getInstance(PhotoResultActivity.this).post(Cache.photoInquiryList,
				VolleyMap.getInstance().photoInquiryList(BitmapBase64, String.valueOf(pageIndex), String.valueOf(pagesize)), PhotoInquiryList.class,
				new VolleyCallback() {
					
					@Override
					public void onSuccess(final VolleyData vd) {
						// TODO Auto-generated method stub
						mPullLayout.loadmoreFinish(mPullLayout.SUCCEED);
					  final List<PhotoInquiryList> list = (List<PhotoInquiryList>)vd.getList();
						if (list.isEmpty()&&pageIndex==1) {
							return;
						}
						JSONObject jsonString = vd.getObj();
						JSONArray jsonArray = jsonString.optJSONArray("data");
						
						if (pageIndex != 1) {
							// 下拉加载，添加数据
							if (jsonArray==null) {
								return;
							}
							try {
								for (int i = 0; i < jsonArray.length(); i++) {

									mainJSONArr.put(jsonArray.get(i));

								}
								mainJson.remove("data");
								mainJson.put("data", mainJSONArr);
							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							adapter.adddata(list);
							mListView
									.setOnItemClickListener(new OnItemClickListener() {

										@Override
										public void onItemClick(
												AdapterView<?> parent,
												View view, int position, long id) {
											Intent intent = new Intent(
													PhotoResultActivity.this,
													CollectionDetailPagerActivity.class);
											intent.putExtra("isOrg", true);
											// 得到新加的item，加入原来的json
											intent.putExtra("id",
													mainJson.toString());
											intent.putExtra("index", position);
											intent.putExtra("titleName", list.get(position).getTitle());
											startActivity(intent);

										}
									});
							return;
						}
						mainJson = jsonString;
						mainJSONArr = jsonArray;
						adapter = new PhotoInquiryAdapter(mContext);
						adapter.setdata(list);
						mListView.setAdapter(adapter);
						mListView
								.setOnItemClickListener(new OnItemClickListener() {

									@Override
									public void onItemClick(
											AdapterView<?> parent, View view,
											int position, long id) {
										Intent intent = new Intent(
												PhotoResultActivity.this,
												CollectionDetailPagerActivity.class);
										intent.putExtra("isOrg", true);
										intent.putExtra("id", vd.getJson());
										intent.putExtra("index", position);
										intent.putExtra("titleName", list.get(position).getTitle());
										startActivity(intent);
									}
								});
						
						
					}
					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub
						mPullLayout.loadmoreFinish(mPullLayout.FAIL);
					}
					});
	}
	
	void onitemclick(){
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(PhotoResultActivity.this, CollectionDetailPagerActivity.class);
				intent.putExtra("id", adapter.listdata.get(position).getArt_id());
				intent.putExtra("titleName", adapter.listdata.get(position).getTitle());
				startActivity(intent);
			}
		});
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
//		TakePhoteActivity.baseImg  = "";
	}
}
